package com.tangding.hprose.api;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @description 
 *
 * @author Ranger
 * @version 2017年11月21日_下午12:54:49
 *
 */
@Component
public class HproseApiHelper {
	
	@Autowired(required = false)
	public List<HproseBase> hps;

	public Map<String, Class<?>> findStaticMethod() {
		Map<String, Class<?>> apiList = new HashMap<>();
		hps.forEach(apiClass -> {
			Method[] methods = apiClass.getClass().getDeclaredMethods();
			for (Method method : methods) {
				if (isStaticMethod(method)) {
					apiList.put(method.getName(), method.getDeclaringClass());
				}
			}
		});
		return apiList;
	}
	
	public static Map<String, Class<?>> findStaticMethod(Class<?> apiClass) {
		Map<String, Class<?>> apiList = new HashMap<>();
		Method[] methods = apiClass.getDeclaredMethods();
		for (Method method : methods) {
			if (isStaticMethod(method)) {
				apiList.put(method.getName(), method.getDeclaringClass());
			}
		}
		return apiList;
	}

	public static boolean isStaticMethod(Method method) {
		String m = method.toGenericString().split(" ")[0];
		if ("static".equals(m)) {
			return true;
		}
		m = method.toGenericString().split(" ")[1];
		if ("static".equals(m)) {
			return true;
		}
		return false;
	}
}
